package com.diyunapp.xilinapp.adapters;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.YouLikeGoodsBean;
import com.diyunapp.xilinapp.myapi.OnRecyclerViewItemClickListener;
import com.diyunapp.xilinapp.selfdefine.MyViewHolder;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * Created by NIDAYE on 2016/6/15.
 */
public class RecycleViewGridAdapter extends RecyclerView.Adapter<MyViewHolder> implements View.OnClickListener{
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;

    public List<YouLikeGoodsBean> mDatas;
    private View headView;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private ChangeGridLayoutManagerSpance changeGridLayoutManager;
    private boolean isAddFoot=false;
    private boolean isAddHead=false;

    //定义实现onItemClickClickListener 的接口
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface ChangeGridLayoutManagerSpance{
        public void change(int size, boolean isAddHead, boolean isAddFoot);
    }
    //提供接口给 让LayoutManager根据添加尾部 头部与否来做判断 显示头部与底部的SpanSize要在添加头部和尾部之后
    public void setChangeGridLayoutManager(ChangeGridLayoutManagerSpance changeGridLayoutManager){
        this.changeGridLayoutManager=changeGridLayoutManager;
        changeGridLayoutManager.change(getItemCount()-1,isAddHead,isAddFoot);
    }

    public RecycleViewGridAdapter(List<YouLikeGoodsBean> datas) {
        mDatas = datas;
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead=true;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot=true;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize==1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize==1 && position == getItemCount()-1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = null;
        switch (i) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = View.inflate(viewGroup.getContext(), R.layout.home_youlike_item_layout, null);
                view.setOnClickListener(this);
                break;

            case TYPE_FOOT:
                view =footView;
                break;
        }
        return new MyViewHolder(view,i);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        if(getItemViewType(position)==TYPE_ITEM)
        {
            int dataPosition = position - headViewSize;
            YouLikeGoodsBean currentdata = mDatas.get(dataPosition);
            ImageLoader.getInstance().displayImage(currentdata.getGoodImgUrl(), holder.goods_picture_iv, FormatConfirm.getOption());
            holder.goods_title_tv.setText(currentdata.getGoodKeyInfo());
            holder.goods_price_tv.setText("￥"+currentdata.getGoodPrice());
            holder.goods_salenumbs_tv.setText("销量："+currentdata.getGoodSaleNums());
            //将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(dataPosition);
        }
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size() + headViewSize + footViewSize;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}



